//
//  AppDelegate.m
//  HONGQIYUN
//
//  Created by 余海华 on 2016/12/19.
//  Copyright © 2016年 Haidi. All rights reserved.
//

#import "AppDelegate.h"
#import "SplashView.h"
#import "GuideViewController.h"
#import <YTKNetwork/YTKNetworkConfig.h>
#import "YTKUrlArgumentsFilter.h"
#import "homeCore.h"
#import "IHomeClient.h"
#import "ZxInfo.h"
#import "LawLibrary.h"
#import "ConvenienceInfo.h"
#import "InteractionInfo.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

+(instancetype)appDelegate
{
    return (AppDelegate*)[[UIApplication sharedApplication]delegate];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    // 网络设置
    [CoreManager initCoreWithDBKey:nil beforeInitCore:^ {
        
        //TODO: coreManager
    }];
    //设置请求配置
    [self setupRequestFilters];
//    [self SetUserAgent];
    
    DHTabBarViewController *tabarVC=[[DHTabBarViewController alloc] init];
    self.navigationController = [[DHNavigationViewController alloc] initWithRootViewController:tabarVC];
    self.tabbarController=tabarVC;
    
    UIViewController *rVC=[[UIViewController alloc]init];
    self.window.rootViewController = rVC;
    
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    [self chooseRootController];
    [self test];
    return YES;
}


-(void) test {
    
    [GetCoreI(IHomeClient) requestInteractionListWith:@"" andPage:@"1" andPageSize:@"10" andKeywords:@"" SuccBlock:^(InteractionInfo *interactionInfo){
        
        
        
    }failBlock:^(NSString *msg){
        
        
        
    }];
    
}
/**
 *
 *  Set user agent (the only problem is that we can't modify the User-Agent later in the program)
 */
-(void)SetUserAgent{
    
    // Set user agent (the only problem is that we can't modify the User-Agent later in the program)
    NSDictionary *dictionnary = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%@/%@/%@(%@; %@)- %@", [[HHNUtility appName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], [HHNUtility DeviceVersion], [HHNUtility systemName], [HHNUtility systemVersion],[HHNUtility LocaleLanguage],requestSetUserAgent], @"UserAgent", nil];
    
    [[NSUserDefaults standardUserDefaults] registerDefaults:dictionnary];
}
/**
 *  设置请求配置
 */
- (void)setupRequestFilters {
    
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    YTKUrlArgumentsFilter *urlFilter = [YTKUrlArgumentsFilter filterWithArguments:@{@"platform":@"ios",@"appVersion": [HHNUtility appVersion]}];
    [config addUrlFilter:urlFilter];
    config.baseUrl= @"http://demo2.hongtian168.com";//@"https://m.hehenian.com";//所有的网络请求都会默认使用 YTKNetworkConfig 中baseUrl参数指定的地址
//    config.cdnUrl = @"https://static.hehenian.com";//对一些静态资源（例如图片、js、css）使用CDN
}


/**
 *  选择根控制器
 */
-(void)chooseRootController
{
    
    
    NSString *key = @"CFBundleVersion";
    // 取出沙盒中存储的上次使用软件的版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *lastVersion = [defaults stringForKey:key];
    // 获得当前软件的版本号
    NSString *currentVersion =[HHNUtility appVersion];
    if ([currentVersion isEqualToString:lastVersion]) {
        
        SplashView *splashView=[[SplashView alloc]init];
        [splashView loadSplashView];
        
    }else{
        
        self.window.rootViewController =[[GuideViewController alloc] init];

    }
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
